package com.ycsj.goldmedalnewconcept.activity;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.UserinfoResp;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.constant.NetConstant;
import com.ycsj.goldmedalnewconcept.pickerview.RegionInfo;
import com.ycsj.goldmedalnewconcept.pickerview.dao.RegionDAO;
import com.ycsj.goldmedalnewconcept.pickerview.view.OptionsPickerView;
import com.ycsj.goldmedalnewconcept.pickerview.view.TimePickerView;
import com.ycsj.goldmedalnewconcept.utils.BitmapCompressor;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.view.ActionSheet;
import com.ycsj.goldmedalnewconcept.view.ActionSheet.MenuItemClickListener;
import com.ycsj.goldmedalnewconcept.R;


//个人资料
public class UserInfoActivity extends Activity implements OnClickListener,MenuItemClickListener{
	private ImageView img_back;
	OptionsPickerView pvOptions;
	protected static final int CHOOSE_PICTURE = 0;
	protected static final int TAKE_PICTURE = 1;
	private static final int CROP_SMALL_PICTURE = 2;
	protected static Uri tempUri;
	private int mWH = 360;// 单位dp
	
	static ArrayList<RegionInfo> item1;

	static ArrayList<ArrayList<RegionInfo>> item2 = new ArrayList<ArrayList<RegionInfo>>();

	static ArrayList<ArrayList<ArrayList<RegionInfo>>> item3 = new ArrayList<ArrayList<ArrayList<RegionInfo>>>();

	private String province;
	private String city;
	private String area;
	private String c;
	private String account;
	private String role;
	private String mRole;
	private Intent intent;
	private ImageView pic1;
	private ImageView pic2;
	private ImageView pic3;
	private String name;
	private LinearLayout ll_myselfphoto;
	private String headimg;
	
	private ImageView iv_user_headimg;
	ArrayList<String> list=new ArrayList<String>();
	ArrayList<String> list2=new ArrayList<String>();
	public int pro_position=0;

	private RelativeLayout rl_user_icon;

	private RelativeLayout rl_user_name;

	private RelativeLayout rl_user_tel;

	private RelativeLayout rl_user_sex;

	private RelativeLayout rl_user_birthday;

	private RelativeLayout rl_user_address;

	private TextView tv_user_name;

	private TextView tv_user_tel;

	private TextView tv_user_sex;

	private TextView tv_user_birthday;

	private TextView tv_user_address;
	
	TimePickerView pvTime;
	
	private String disableGroup;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg)
		{
//			System.out.println(System.currentTimeMillis());
			// 三级联动效果

			pvOptions.setPicker(item1, item2, item3, true);
			pvOptions.setCyclic(true, true, true);
			pvOptions.setSelectOptions(0, 0, 0);
			rl_user_address.setClickable(true);
		};
	};
	



		@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userinfo);
		
		intent = getIntent();
		c = intent.getStringExtra("c");
		role = intent.getStringExtra("role");
		
		mRole = SPUtil.getString(UserInfoActivity.this, "role", "");
		if(c==null&&role==null){
			c = SPUtil.getString(UserInfoActivity.this, "account", "");
			role = SPUtil.getString(UserInfoActivity.this, "role", "");
		}
		if("".equals(c)&&"".equals(role)){
			c = SPUtil.getString(UserInfoActivity.this, "account", "");
			role = SPUtil.getString(UserInfoActivity.this, "role", "");
		}
		disableGroup = SPUtil.getString(UserInfoActivity.this, "disableGroup", "");
		account = SPUtil.getString(UserInfoActivity.this, "account", "");
		initView();
		setOnListener();
		
	}
	
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}



	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
	}


	private void initView() {
		rl_user_icon = (RelativeLayout) findViewById(R.id.rl_user_icon);
		rl_user_name = (RelativeLayout) findViewById(R.id.rl_user_name);
		rl_user_tel = (RelativeLayout) findViewById(R.id.rl_user_tel);
		rl_user_sex = (RelativeLayout) findViewById(R.id.rl_user_sex);
		rl_user_birthday = (RelativeLayout) findViewById(R.id.rl_user_birthday);
		rl_user_address = (RelativeLayout) findViewById(R.id.rl_user_address);
		
		tv_user_name = (TextView) findViewById(R.id.tv_user_name);
		tv_user_tel = (TextView) findViewById(R.id.tv_user_tel);
		tv_user_sex = (TextView) findViewById(R.id.tv_user_sex);
		tv_user_birthday = (TextView) findViewById(R.id.tv_user_birthday);
		tv_user_address = (TextView) findViewById(R.id.tv_user_address);
		
		iv_user_headimg = (ImageView) findViewById(R.id.iv_user_headimg);
		img_back=(ImageView) findViewById(R.id.img_back);
		ll_myselfphoto = (LinearLayout) findViewById(R.id.ll_myselfphoto);
		pic1 = (ImageView) findViewById(R.id.pic1);
		pic2 = (ImageView) findViewById(R.id.pic2);
		pic3 = (ImageView) findViewById(R.id.pic3);
		if("0".equals(mRole)){
			ll_myselfphoto.setVisibility(ll_myselfphoto.GONE);
		}
	}
	
	private void setOnListener() {
		
		img_back.setOnClickListener(this);
		
		if(c.equals(SPUtil.getString(UserInfoActivity.this, "account", ""))&&!c.equals("")){
			rl_user_icon.setOnClickListener(this);
			rl_user_name.setOnClickListener(this);
			rl_user_tel.setOnClickListener(this);
			rl_user_sex.setOnClickListener(this);
			rl_user_birthday.setOnClickListener(this);
			rl_user_address.setOnClickListener(this);
		}
		ll_myselfphoto.setOnClickListener(this);
		
		
		
		// 时间选择器
				pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
				// 控制时间范围
//				 Calendar calendar = Calendar.getInstance();
//				 pvTime.setRange(calendar.get(Calendar.YEAR) - 20,
//				 calendar.get(Calendar.YEAR));
				pvTime.setTime(new Date());
				pvTime.setCyclic(false);
				pvTime.setCancelable(true);
				// 时间选择后回调
				pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener()
				{

					@Override
					public void onTimeSelect(Date date)
					{
						updateUserinfo("生日",getTime(date));
						tv_user_birthday.setText(getTime(date));
						
					}
				});
				
				// 选项选择器
				pvOptions = new OptionsPickerView(this);

				new Thread(new Runnable()
				{

					@Override
					public void run()
					{
						// TODO Auto-generated method stub
//						System.out.println(System.currentTimeMillis());
						if (item1 != null && item2 != null && item3 != null)
						{
							handler.sendEmptyMessage(0x123);
							return;
						}
						item1 = (ArrayList<RegionInfo>) RegionDAO.getProvencesOrCity(1);
						for (RegionInfo regionInfo : item1)
						{
							item2.add((ArrayList<RegionInfo>) RegionDAO.getProvencesOrCityOnParent(regionInfo.getId()));

						}

						for (ArrayList<RegionInfo> arrayList : item2)
						{
							ArrayList<ArrayList<RegionInfo>> list2 = new ArrayList<ArrayList<RegionInfo>>();
							for (RegionInfo regionInfo : arrayList)
							{

								

								ArrayList<RegionInfo> q = (ArrayList<RegionInfo>) RegionDAO.getProvencesOrCityOnParent(regionInfo.getId());
								list2.add(q);

							}
							item3.add(list2);
						}

						handler.sendEmptyMessage(0x123);

					}
				}).start();
				// 设置选择的三级单位
				// pwOptions.setLabels("省", "市", "区");
				pvOptions.setTitle("选择城市");

				// 设置默认选中的三级项目
				// 监听确定选择按钮

				pvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener()
				{

					

					@Override
					public void onOptionsSelect(int options1, int option2, int options3)
					{
						// 返回的分别是三个级别的选中位置
						String tx = item1.get(options1).getPickerViewText() + item2.get(options1).get(option2).getPickerViewText() + item3.get(options1).get(option2).get(options3).getPickerViewText();
						province = item1.get(options1).getPickerViewText();
						city = item2.get(options1).get(option2).getPickerViewText();
						area = item3.get(options1).get(option2).get(options3).getPickerViewText();
						updateUserinfo("所在地",tx);
						tv_user_address.setText(tx);

					}
				});
				
			
				
		
	}

	private void initData() {
		
		OkHttpClient mOkHttpClient = new OkHttpClient();
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "5");
		builder.add("c", c);
		builder.add("role", role);
		builder.add("platform", "2");
		Request request = new Request.Builder()
		.url(Constant.SERVER_URL + "AccountMsg")
		.post(builder.build()).build();
mOkHttpClient.newCall(request).enqueue(callBack);
	}
	Handler handler1=new Handler();
	Callback callBack=new Callback() {

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			
		}

		@Override
		public void onResponse(Response arg0) throws IOException {
			String json =arg0.body().string();
			Log.e("test", json);
			Gson gson=new Gson();
			final UserinfoResp fromJson = gson.fromJson(json, UserinfoResp.class);
			if(fromJson!=null&&!"-1".equals(fromJson.state)){
			handler1.postDelayed(new Runnable() {
				
				
				@Override
				public void run() {
					tv_user_name.setText(fromJson.name);
					
					if(fromJson.tel!=null&&fromJson.tel.length()>10){
						
					
					//隐藏电话号码
					if("0".equals(disableGroup)&&fromJson.tel.length()>10&&!account.equals(fromJson.tel)){
						tv_user_tel.setText(fromJson.tel.substring(0,3)+"*****"+fromJson.tel.substring(8,fromJson.tel.length()));
					}else{
						tv_user_tel.setText(fromJson.tel);	
					}
					}
					
					
					name = fromJson.name;
					if(fromJson.pic!=null&&fromJson.pic.size()!=0){
						
					
					if(fromJson.pic.get(0).url!=null&&!"".equals(fromJson.pic.get(0).url)){
						Picasso.with(UserInfoActivity.this).load(fromJson.pic.get(0).url).into(pic1);
					}else{
						pic1.setVisibility(pic1.GONE);
					}
					if(fromJson.pic.get(1).url!=null&&!"".equals(fromJson.pic.get(1).url)){
						Picasso.with(UserInfoActivity.this).load(fromJson.pic.get(1).url).into(pic2);
					}else{
						pic2.setVisibility(pic2.GONE);
					}
					if(fromJson.pic.get(2).url!=null&&!"".equals(fromJson.pic.get(2).url)){
						Picasso.with(UserInfoActivity.this).load(fromJson.pic.get(2).url).into(pic3);
					}else{
						pic3.setVisibility(pic3.GONE);
					}
					}
					if("0".equals(fromJson.sex)){
						
						tv_user_sex.setText("女");
					}else{
						tv_user_sex.setText("男");
					}
					tv_user_birthday.setText(fromJson.birthday);
					tv_user_address.setText(fromJson.province+"-"+fromJson.city);
					Picasso.with(UserInfoActivity.this).load(fromJson.icon).error(R.drawable.header).into(iv_user_headimg);
					
					headimg = fromJson.icon;
					
					
					if(c.equals(SPUtil.getString(UserInfoActivity.this, "account", ""))
							&&role.equals(SPUtil.getString(UserInfoActivity.this, "role", ""))){
						SPUtil.putString(UserInfoActivity.this, "icon", headimg);
						SPUtil.putString(UserInfoActivity.this, "name", fromJson.name);
					}
					pic1.setVisibility(pic1.VISIBLE);
					pic2.setVisibility(pic2.VISIBLE);
					pic3.setVisibility(pic3.VISIBLE);
				}
			}, 0);
			}
		}
		
	};
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		case R.id.rl_user_icon:
			try {
				showChoosePicDialog();
			} catch (Exception e) {
				Log.e("error", e.toString());
			}
			break;
		case R.id.rl_user_name:
			Intent intent1=new Intent(UserInfoActivity.this, UpdateUserinfoActivity.class);
			intent1.putExtra("title", "姓名");
			intent1.putExtra("sex", tv_user_sex.getText().toString());
			startActivity(intent1);
			break;
		case R.id.rl_user_tel:
			Intent intent=new Intent(UserInfoActivity.this, UpdateUserinfoActivity.class);
			intent.putExtra("title", "电话");
			intent.putExtra("sex", tv_user_sex.getText().toString());
			startActivity(intent);
			break;
		case R.id.rl_user_sex:
			setTheme(R.style.ActionSheetStyleIOS7);
			 showActionSheet();
			break;
		case R.id.rl_user_birthday:
			pvTime.show();
			break;
		case R.id.rl_user_address:
			pvOptions.show();
			break;
		case R.id.ll_myselfphoto:
			Intent intent2 = new Intent(this,UserQuanziActivity.class);
			intent2.putExtra("c", c);
			intent2.putExtra("role", role);
			intent2.putExtra("name",name);
			intent2.putExtra("headimg",headimg);
			startActivity(intent2);
			break;
		default:
			break;
		}
	}
	private ActionSheet menuView;
	  
	  public void showActionSheet()
		{
			menuView = new ActionSheet(this);
			menuView.setCancelButtonTitle("cancel");// before add items
			menuView.addItems("男", "女");
			menuView.setItemClickListener(this);
			menuView.setCancelableOnTouchMenuOutside(true);
			menuView.showMenu();
		}
	  @Override
		public void onItemClick(int itemPosition) {
			tv_user_sex.setText(menuView.returnCancelButtonTitle(itemPosition));
			String sex = menuView.returnCancelButtonTitle(itemPosition);
			
			if("男".equals(sex)){
				updateUserinfo("性别","true");
			}else if("女".equals(sex)){
				updateUserinfo("性别","false");
			}
			
			
			
//			Toast.makeText(this, menuView.returnCancelButtonTitle(itemPosition), 0).show();
		}
	  
	  public static String getTime(Date date){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			return format.format(date);
		}
	  
	  
	 public void updateUserinfo(String key,String value){

//			Toast.makeText(UserInfoActivity.this, value, 0).show();
			OkHttpClient mOkHttpClient = new OkHttpClient();
//			mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			FormEncodingBuilder builder = new FormEncodingBuilder();
			builder.add("type", "4");
			builder.add("c", SPUtil.getString(UserInfoActivity.this, "account", ""));
			builder.add("role", SPUtil.getString(UserInfoActivity.this, "role", ""));
			builder.add("platform", "2");
			builder.add("name", "");
			builder.add("tel", "");
//http://pm.moregolden.com/ycsj_platform/AccountMsg?type=4&c=18271261779&role=1&platform=2&name=&tel=2&sex=false&province=&city=&area=&birthday=
			
				if("男".equals(tv_user_sex.getText().toString())){
					builder.add("sex", "true");
				}else{
					builder.add("sex", "false");
				}
			if("所在地".equals(key)){
				builder.add("province", province);
				builder.add("city", city);
				builder.add("area", area);
			}else{
				builder.add("province", "");
				builder.add("city", "");
				builder.add("area", "");
			}
			if("生日".equals(key)){
				builder.add("birthday", value);
			}else{
				builder.add("birthday", "");
			}
			if("1".equals(role)){
				builder.add("favorite", "");
			}
			
			Request request = new Request.Builder()
			.url(Constant.SERVER_URL + "AccountMsg")
			.post(builder.build()).build();
	mOkHttpClient.newCall(request).enqueue(new Callback() {

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			
		}

		@Override
		public void onResponse(Response arg0) throws IOException {
			Log.e("test", arg0.body().string());
			initData();
		}
		
	});
			
		
		 
	  }
	  
	  
	 
	 
	 protected void showChoosePicDialog() {
		 Date date=new Date();
		 SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
		 String format = dateFormat.format(date);
		 pngName = SPUtil.getString(this, "account", "")+SPUtil.getString(this, "role", "")+format;
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("设置头像");
			String[] items = { "选择本地图片", "拍照" };
			builder.setNegativeButton("取消", null);
			builder.setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					switch (which) {
					case CHOOSE_PICTURE: 
						Intent openAlbumIntent = new Intent(
								Intent.ACTION_GET_CONTENT);
						openAlbumIntent.setType("image/*");
						// Data = Uri.fromFile(new File(Environment
						// .getExternalStorageDirectory(), "image1.jpg"));
						startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
						break;

					case TAKE_PICTURE: 
						Intent openCameraIntent = new Intent(
								MediaStore.ACTION_IMAGE_CAPTURE);
						tempUri = Uri.fromFile(new File(Environment
								.getExternalStorageDirectory(), "image.jpg"));
						openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
						startActivityForResult(openCameraIntent, TAKE_PICTURE);
						break;
					}
				}
			});
			builder.create().show();
		}
	 Bitmap bitmap2headimg=null;
		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			super.onActivityResult(requestCode, resultCode, data);
			if (resultCode == RESULT_OK) { 
				switch (requestCode) {
				case TAKE_PICTURE:
					startPhotoZoom(tempUri); 
					break;
				case CHOOSE_PICTURE:
					Uri uri = data.getData();
					Bitmap bitmap = null;
					String imgPath = null;
					ContentResolver resolver = this.getContentResolver();
					String[] columns = { MediaStore.Images.Media.DATA };
					Cursor cursor = null;
					cursor = resolver.query(uri, columns, null, null, null);
					imgPath = uri.getPath();
					if (!TextUtils.isEmpty(imgPath) && imgPath.contains(":")) {
						String imgIndex = imgPath.split(":")[1];
						cursor = resolver.query(
								MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
								columns, "_id=?", new String[] { imgIndex }, null);
					}
					if (null != cursor && cursor.moveToFirst()) {
						int columnIndex = cursor.getColumnIndex(columns[0]);
						imgPath = cursor.getString(columnIndex);
						cursor.close();
						String filePath = imgPath;
					}
					if (!TextUtils.isEmpty(imgPath)) {
						bitmap = genBitmap(imgPath);
						bitmap2headimg=genBitmap(imgPath);
//						imageView2.setImageBitmap(bitmap);
						upload();
					}
					break;
				case CROP_SMALL_PICTURE:
					if (data != null) {
						setImageToView(data);
					}
					break;
				}
			}
		}
		
		
		/**
		 * @param picdata
		 */
		protected void setImageToView(Intent data) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				Bitmap photo = extras.getParcelable("data");
				iv_user_headimg.setImageBitmap(photo);
				try {
					saveFile(photo, pngName+".png");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				upload();
			}
		}
		/**
		 * 
		 * @param uri
		 */
		protected void startPhotoZoom(Uri uri) {
			if (uri == null) {
				Log.i("tag", "The uri is not exist.");
			}
			tempUri = uri;
			Intent intent = new Intent("com.android.camera.action.CROP");
			intent.setDataAndType(uri, "image/*");
			intent.putExtra("crop", "true");
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("outputX", 150);
			intent.putExtra("outputY", 150);
			intent.putExtra("return-data", true);
			startActivityForResult(intent, CROP_SMALL_PICTURE);
		}
		
		/** 通过给定的图片路径生成对应的bitmap */
		private Bitmap genBitmap(String imgPath) {
			Options options = new Options();
			// options.inJustDecodeBounds = true;

			// 将图片像素设置为低像素
			options.inDensity = DisplayMetrics.DENSITY_HIGH;
			// 降低图片质量 设置为16位
			options.inPreferredConfig = Config.ARGB_8888;

			BitmapFactory.decodeFile(imgPath, options);
			int imageWidth = options.outWidth>100?100:options.outWidth;
			int imageHeight = options.outHeight>100?100:options.outHeight;

			int widthSample = (int) (imageWidth / mWH);
			int heightSample = (int) (imageHeight / mWH);
			// 计算缩放比例
			// options.inSampleSize = widthSample < heightSample ? heightSample
			// : widthSample;
			// options.inSampleSize=2;
			if (imageWidth < 2048 || imageHeight < 2048) {
				options.inSampleSize = 1;
			} else if (imageWidth > 2048 || imageHeight > 2048) {
				options.inSampleSize = 2;
			}
			// options.inJustDecodeBounds = false;
			Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
			bitmap = BitmapCompressor.decodeSampledBitmapFromBitmap(bitmap,
					imageWidth / (options.inSampleSize ), imageHeight
							/ (options.inSampleSize ));

			try {
				saveFile(bitmap, pngName+".png");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return bitmap;
		}
		
		
		/**
		 * 保存文件
		 * 
		 * @param bm
		 * @param fileName
		 * @throws IOException
		 */
		public void saveFile(Bitmap bm, String fileName) throws IOException {
			String path = getSDPath() + "/goldupload/";
			File dirFile = new File(path);
			if (!dirFile.exists()) {
				dirFile.mkdir();
			}

			File myCaptureFile = new File(path + fileName);
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(myCaptureFile));
			bm.compress(Bitmap.CompressFormat.PNG, 80, bos);
			bos.flush();
			bos.close();
		}

		public String getSDPath() {
			File sdDir = null;
			boolean sdCardExist = Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
			if (sdCardExist) {
				sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
			} else {
				sdDir = getFilesDir();
			}
			return sdDir.toString();
		}
		
		private HttpUtils httpUtils;
		private String url;
		private RequestParams params;
		
		public void upload() {
			 httpUtils = new HttpUtils();
//				url = "http://pm.moregolden.com/ycsj_count/MessageCenter";
				url = Constant.ycsj_count_MessageCenter;
				params = new RequestParams();
				params.addBodyParameter("type","8");
				params.addBodyParameter("c",SPUtil.getString(UserInfoActivity.this, "account",""));
				params.addBodyParameter("role",SPUtil.getString(UserInfoActivity.this, "role",""));
				params.addBodyParameter("platform","2");

				String path = getSDPath() + "/goldupload/" + pngName+".png";
				File f = new File(path);
//				Log.e("test", "2234" + path);
				String[] split = path.split("\\/");
//				String name = split[split.length - 1];
				String name =pngName+".png";
				params.addBodyParameter(name, f,"image/png");
				params.addBodyParameter("fileinfo", name);
				
				httpUtils.send(HttpMethod.POST, url, params, callBackHeadimg);
		}
		
		
		private RequestCallBack<String> callBackHeadimg = new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				
				String json = responseInfo.result;
				Log.e("test", json);
				// 使用Gson把json解析成对象
				// 1 得到gson对象
				Gson gson = new Gson();
				// 2 使用gson对象的方法
				// NewsColumnResp newsColumnResp = gson.fromJson(json,
				// NewsColumnResp.class);
//				Log.d("test", "上传成功" + json);
				Toast.makeText(UserInfoActivity.this, "上传成功", 0).show();
				initData();
				
				
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				// Toast.makeText(MainActivity.this, "onFailure", 0).show();
//				Log.d("test", "" + error.getMessage());
			}
		};
		private String pngName;
}
